#ifndef __UDSF_PROTOCOL_DEBUG_C__
#define __UDSF_PROTOCOL_DEBUG_C__

#include "app.h"

/*
*   notice:
*   UDSF 同UDF编程：不可调用cstd-lib,只可调用"sdk_ifs_udk_cfg.h"、"sys_api.h"中列出的API函数。
*   UDSF文件内函数均需使用static修饰符，且UDF中以#include "udsf_xxx.c"形式引用
*/

#include "udsf_uart.c"
#include "udsf_spi.c"
#include "udsf_delay.c"

// 最后修改：2023年6月5日 09点32分 V1.0

/*
 * protocol-debug define
*/
typedef enum{
	eFRAME_TIMING_8BIT = 0,
	eFRAME_TIMING_PSEUDO_FLOATING = 1,
	eFRAME_FFT1D_PSEUDO_FLOATING = 2,
	eFRAME_FFT2D_PSEUDO_FLOATING = 3,
	eFRAME_CFAR = 4,
	eFRAME_POINTRST = 5,
}eFRAME_class_t;

typedef enum{
	eDRAW_SMALL_FRAME = 0,  // chirp帧
	eDRAW_CHIRP_SUM = 1,    // chirp和
	eDRAW_FRAME = 2,        // chirps
	eDRAW_FFT1D = 3,        // fft1d
	eDRAW_FFT1D_MEAN = 4,   // fft1d 均值
	eDRAW_FFT1D_MINUS = 5,  // fft1d 减均值
	eDRAW_FFT2D = 6,        // fft2d
	eDRAW_CFAR_VEL = 7,     // cfar 速度向
	eDRAW_CFAR_RANGE = 8,   // cfar 距离向
	eDRAW_CFAR_RV = 9,      // cfar 距离向
	eDRAW_POINTRST = 10,    // fft1d 点云结果
	eDRAW_DBSCAN  = 11,     // fft1d DBSCAN结果
	eDRAW_FFT1D_FRAME_DIFF = 12, // fft1d 帧间相减
	eDRAW_ZDPPLR = 13, // 零多普勒
	eDRAW_ZDPPLR_DIFF = 14,// 零多普勒帧间差
}eDraw_name_t;

typedef enum{
	eDRAW_ANTALL = 0,           // 所有天线
	eDRAW_ANT1 = 1,             // 天线1
	eDRAW_ANT2 = 2,             // 天线2
	eDRAW_ANT1_CALIB = 3,       // 天线1校正
	eDRAW_ANT2_CALIB = 4,       // 天线2校正
	eDRAW_ANT1_ADD_ANT2 = 5,    // 天线1+天线2
	eDRAW_ANT1_MINUS_ANT2 = 6,  // 天线1-天线2
}eDraw_name_id_t;

struct proto_data_frame_info {
	uint8_t version;
	uint8_t interval_10_100_ms;
	uint16_t data_len;

	uint8_t pack_total;
	uint8_t pack_curr;	
	eFRAME_class_t frame_class;
	uint8_t samp_ant;
	
	uint16_t samp_num;
	uint16_t samp_chirp;
	
	uint8_t draw_id;
	eDraw_name_t draw_name;
	eDraw_name_id_t draw_name_id;
	uint8_t draw_opt;
};

typedef void (*FUNC_DATA_SEND_t)(uint8_t*,int);

struct proto_data_cfg{
	uint8_t* pDataSrc;
	uint32_t dataSrc_inc;
	uint16_t DataSrc_ChirpInc;
	FUNC_DATA_SEND_t send_frame;
	FUNC_DATA_SEND_t send_data;
	struct proto_data_frame_info frame_info;
	uint8_t Proto_MVersion;
};

#define FRAME_INFO_VERSION (0x20)


static void udsf_protocol_debug_send(uint8_t chirp_mode,struct proto_data_cfg *pFrame,int data_len,int total_pkg,int curr_pkg,int chirp_interval) __attribute__((unused));

// chirp_mode 0-行存储 其它-列存储间隔
static void udsf_protocol_debug_send(uint8_t chirp_mode,struct proto_data_cfg *pFrame,int data_len,int total_pkg,int curr_pkg,int chirp_interval)
{	
	struct proto_data_frame_info * pFrameInfo = &pFrame->frame_info;
	
	pFrameInfo->version = FRAME_INFO_VERSION;
	uint8_t bytes_num = 4;

	if( pFrameInfo->frame_class == eFRAME_TIMING_8BIT ){
		bytes_num = 1;
	}else if( (pFrameInfo->frame_class == eFRAME_CFAR) || (pFrameInfo->frame_class == eDRAW_POINTRST) ){
		bytes_num = 8;
	}
	
	uint32_t chirp_len = pFrameInfo->samp_num * pFrameInfo->samp_ant * bytes_num;
	if( data_len < 0 ){
		pFrameInfo->data_len = chirp_len * pFrameInfo->samp_chirp;
	}else{
		pFrameInfo->data_len = data_len;
	}

	pFrameInfo->pack_curr  = curr_pkg;
	pFrameInfo->pack_total = total_pkg;
	
	if( pFrame->Proto_MVersion > 1){
		uint8_t frame_head = 0xfe;
		pFrame->send_frame((uint8_t*)&frame_head,1);
		pFrame->send_data((uint8_t*)pFrameInfo,sizeof(struct proto_data_frame_info));
	}else if( pFrame->Proto_MVersion == 1 ){
		uint8_t frame_head[]={1,2,3,4,5,6,7,8};
		pFrame->send_frame(frame_head,sizeof(frame_head));
	}

	if( chirp_mode != 0 ){ // 列模式
			for( int i=0;i<pFrameInfo->samp_chirp;i++){
				for( int j=0;j<pFrameInfo->samp_ant;j++){
					for( int k=0;k<pFrameInfo->samp_num;k++ ){
						pFrame->send_data((uint8_t*)pFrame->pDataSrc +j*pFrame->DataSrc_ChirpInc + k*bytes_num*chirp_mode + bytes_num*i,
						bytes_num);
				}
			}
		}
	}
	else{
		if( chirp_interval == 0 ){
			pFrame->send_data((uint8_t*)pFrame->pDataSrc,pFrameInfo->data_len);
		}else{
			for( int i=0;i<pFrameInfo->samp_chirp;i++){
				pFrame->send_data((uint8_t*)pFrame->pDataSrc+i*chirp_interval,chirp_len);
			}
		}
	}
	if( pFrame->Proto_MVersion > 1 ){
		uint8_t frame_tail = 0xff;
		pFrame->send_frame((uint8_t*)&frame_tail,1);
	}
	
	if( curr_pkg == total_pkg ){
		pFrame->frame_info.draw_id++;
	}
}


#endif
